﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WPFFolderBrowser;
using System.Windows.Forms.Integration;
using AxShockwaveFlashObjects;
using mshtml;
using ScormModel;
using System.IO;

namespace WpfELearningAuthoringTool
{
    /// <summary>
    /// Interaction logic for InsertVideoDialog.xaml
    /// </summary>
    public partial class InsertVideoDialog : Window
    {
         private IHTMLDocument2 doc;
        string currentDirectory;
        Resource re;
        public IHTMLDocument2 DocManager
        {
            get { return this.doc; }
            set { this.doc = value; }
        }
        public Resource SelectResource
        {
            get { return this.re; }
            set { this.re = value; }
        }
        public string CurrentDirectory
        {
            get { return this.currentDirectory; }
            set { this.currentDirectory = value; }
        }
        public InsertVideoDialog()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.DefaultExt = ".mp4";
                dlg.Filter = "Media files(.mp4)(.wmv)|*.mp4;*.wmv";
                Nullable<bool> result = dlg.ShowDialog();
                if (result == true)
                {
                    txtFlashPath.Text = dlg.FileName;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnPreview_Click(object sender, RoutedEventArgs e)
        {
            if (txtFlashPath.Text.Length == 0)
            {
                MessageBox.Show("Please select video file");
                return;
            }
            try
            {
                //if (txtFlashPath.Text.Contains(".mp4"))
                //{
                    videoView.Children.Clear();
                    string strFilePath = txtFlashPath.Text;
                    MediaElement me = new MediaElement();
                    me.LoadedBehavior = MediaState.Manual;
                    videoView.Children.Add(me);
                    me.Source = new Uri(strFilePath);
                    me.Play();
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(currentDirectory + "/media/" + System.IO.Path.GetFileName(txtFlashPath.Text)))
                {
                    File.Copy(txtFlashPath.Text, currentDirectory + "/media/" + System.IO.Path.GetFileName(txtFlashPath.Text));
                }
                string flashScript = MediaUtil.buildMediaScript(txtFlashPath.Text, txtFlashWidth.Text, txtFlashHeight.Text);
                var element = doc.createElement("div");
                element.innerHTML = flashScript;
                doc.body.insertAdjacentHTML("beforeEnd", element.outerHTML);
                //check duplicate resource
                bool test = false;
                for (int i = 0; i < re.ListFileRef.Count; i++)
                {
                    if (re.ListFileRef[i].Bref.Contains(System.IO.Path.GetFileName(txtFlashPath.Text)))
                        test = true;
                    else
                        continue;
                }
                if (test == false)
                {
                    FileRef fr = new FileRef();
                    fr.Bref = "media/"+System.IO.Path.GetFileName(txtFlashPath.Text);
                    re.ListFileRef.Add(fr);
                }
               // MessageBox.Show("create flash success");
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("create video fail");
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
